package com.dhl.fin.api.service;

import com.dhl.fin.api.common.dto.QueryDto;
import com.dhl.fin.api.common.enums.ActionEnum;
import com.dhl.fin.api.common.enums.CacheKeyEnum;
import com.dhl.fin.api.common.service.CommonService;
import com.dhl.fin.api.common.util.*;
import com.dhl.fin.api.domain.Dictionary;
import com.dhl.fin.api.domain.Project;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author becui
 * @date 4/1/2020
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DictionaryServiceImpl extends CommonService<Dictionary> {

    @Autowired
    ProjectServiceImpl projectService;


    @Override
    public void beforeSave(Dictionary domain) throws Exception {
        String[] childNames = WebUtil.getStringArrayParam("childName");
        String[] childCodes = WebUtil.getStringArrayParam("childCode");
        Long projectId = WebUtil.getLongParam("projectId");
        Long[] childIds = WebUtil.getLongArrayParam("childId");


        if (this.actionEnum.equals(ActionEnum.UPDATE)) {
            List<Dictionary> dictionaries = select(QueryDto.builder().addWhere("parent_id = " + domain.getId()).build());
            for (Dictionary dictionary : dictionaries) {
                saveOrUpdate(dictionary);
            }
        }

        Dictionary childDictionary;
        if (ObjectUtil.notNull(childNames) && ObjectUtil.notNull(childCodes)) {
            for (int i = 0; i < childNames.length; i++) {
                Long id = ObjectUtil.isNull(childIds) || childIds.length - 1 < i ? null : childIds[i];
                childDictionary = new Dictionary(id, childNames[i], childCodes[i], domain, new Project(projectId));
                if (CollectorUtil.isEmpty(domain.getChildren())) {
                    domain.setChildren(new LinkedList<>());
                }

                domain.getChildren().add(childDictionary);
            }
        }

    }

    @Override
    public void beforePageQuery(QueryDto pageQuery) throws Exception {
        pageQuery.getJoinDomain().add("parent");
        pageQuery.getWhereCondition().add("parent.id is null");
    }

    public Map<String, List<Dictionary>> getDictionaryPerProject() throws Exception {
        List<Project> projects = projectService.select(QueryDto.builder().available().build());
        MapBuilder<String, List<Dictionary>> mapBuilder = MapUtil.builder();
        if (CollectorUtil.isNoTEmpty(projects)) {
            for (Project project : projects) {
                mapBuilder.add(project.getCode(), getAllDictionary(project.getCode()));
            }
        }
        return mapBuilder.build();
    }

    public List<Dictionary> getAllDictionary(String projectCode) throws Exception {

        List<Dictionary> parentDic = select(QueryDto.builder()
                .available()
                .addWhere("parent.id is null")
                .addWhere(String.format("project.code = '%s'", projectCode))
                .build());

        for (Dictionary dictionary : parentDic) {
            List<Dictionary> children = select(QueryDto.builder()
                    .available()
                    .addWhere("parent.id =" + dictionary.getId()
                    ).build());
            dictionary.setChildren(children);
        }

        return parentDic;
    }

    public List<Dictionary> getAllDictionary() throws Exception {

        List<Dictionary> parentDic = select(QueryDto.builder()
                .available()
                .addWhere("parent.id is null")
                .build());

        for (Dictionary dictionary : parentDic) {
            List<Dictionary> children = select(QueryDto.builder()
                    .available()
                    .addWhere("parent.id =" + dictionary.getId()
                    ).build());
            dictionary.setChildren(children);
        }

        return parentDic;
    }


}



